package com.example.hao.ximalaya.adapter.viewholder;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.hao.ximalaya.R;
import com.example.hao.ximalaya.pojo.HotData;
import com.example.hao.ximalaya.pojo.Type23listItem;
import com.example.hao.ximalaya.utils.NumberUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class Type23ViewHolder extends RecyclerView.ViewHolder {


    private Context context;


    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    private RVItemAdapter rvItemAdapter;

    @OnClick(R.id.tv_more)
    public void more() {
        Toast.makeText(context, "点击了更多", Toast.LENGTH_SHORT).show();
    }

    @BindView(R.id.rv_item)
    RecyclerView rvItem;

    @OnClick(R.id.ll_change)
    public void change() {
        Toast.makeText(context, "点击了换一批", Toast.LENGTH_SHORT).show();
    }

    public Type23ViewHolder(Context context, View itemView) {
        super(itemView);
        this.context = context;
        ButterKnife.bind(this, itemView);
        initView();
    }

    private void initView() {
        rvItem.setLayoutManager(new GridLayoutManager(context, 3, LinearLayoutManager.VERTICAL, false));
        rvItemAdapter = new RVItemAdapter(context);
        rvItem.setAdapter(rvItemAdapter);

    }

    /**
     * 为 ViewHolder 绑定数据
     *
     * @param data 数据对象
     */
    ArrayList<Type23listItem> listItems;
    public void setData(HotData.ListBeanX data) {
        List<JsonObject> list = data.getList();
        listItems = new ArrayList<>();
        for (JsonObject obj : list) {
            Type23listItem type23listItem = new Gson().fromJson(obj, Type23listItem.class);
            listItems.add(type23listItem);
        }
        rvItemAdapter.setListItems(listItems);
        tvTitle.setText(data.getTitle());
    }

    class RVItemAdapter extends RecyclerView.Adapter<RVItemAdapter.ItemViewHolder> {


        private Context context;
        private List<Type23listItem> listItems;

        public RVItemAdapter(Context context) {
            this.context = context;
        }

        public void setListItems(List<Type23listItem> listItems) {
            this.listItems = listItems;
        }

        @NonNull
        @Override
        public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View layout = LayoutInflater.from(context).inflate(R.layout.view_rv_item_type_23_rv_item, parent, false);

            return new ItemViewHolder(layout);
        }

        @Override
        public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {

            Type23listItem type23listItem = listItems.get(position);
            holder.tvTitle.setText(type23listItem.getTitle());
            double commentScore = type23listItem.getCommentScore();
            String s = NumberUtils.formatDouble(commentScore);
            holder.tvNum.setText(s);
            Glide.with(context).load(type23listItem.getPic()).into(holder.ivIcon);
            if (type23listItem.getVipFreeType() == 0) {
                holder.tv_jp.setVisibility(View.VISIBLE);
                holder.tv_VIP.setVisibility(View.GONE);
            }else {
                holder.tv_jp.setVisibility(View.GONE);
                holder.tv_VIP.setVisibility(View.VISIBLE);
            }

        }

        @Override
        public int getItemCount() {
            return listItems == null ? 0 : listItems.size();
        }

        class ItemViewHolder extends RecyclerView.ViewHolder {

            @BindView(R.id.iv_icon)
            ImageView ivIcon;
            @BindView(R.id.tv_num)
            TextView tvNum;
            @BindView(R.id.rv_icon)
            CardView rvIcon;
            @BindView(R.id.tv_title)
            TextView tvTitle;
            @BindView(R.id.tv_jp)
            TextView tv_jp;
            @BindView(R.id.tv_VIP)
            TextView tv_VIP;

            public ItemViewHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
                int widthPixels = context.getResources().getDisplayMetrics().widthPixels;
                ViewGroup.LayoutParams lp = rvIcon.getLayoutParams();
                int spacingWidth = 15;
                lp.width = (widthPixels - 8 * spacingWidth) / 3;
                lp.height = (widthPixels - 8 * spacingWidth) / 3;
                rvIcon.setLayoutParams(lp);
            }
        }
    }
}
